/*$('.big-img').on('load', function () {
 $('.small-img').remove();//从模糊到清晰
 });*/


(function ($, win) {
  //字数限制
  $.fn.limitWords = function (max) {
    this.each(function () {
      var text = $.text($(this));
      if (text.length > max) {
        this.innerHTML = text.substring(0, max) + '...';
      }
    });
    return this;
  };
  //slide淡入淡出轮播
  $.fn.fadingSlide = function (options) {
    var settings = $.extend({
        'speed': '3000'
      }, options),
      that = $(this),
      btn = that.find('ul.slide-btn').find('li'),
      imgLi = that.find('ul.slide-img').find('li'),
      ind = 0,
      length = btn.length,
      timer = null;

    function play(ind) {
      btn.removeClass('on').eq(ind).addClass('on');
      imgLi.removeClass('up').eq(ind).addClass('up');
    }

    function witchplay(witch) {
      if (witch > 0) {
        if (ind === length - 1) {
          ind = 0;
          play(ind);
        } else {
          ind++;
          play(ind);
        }
      } else {
        if (ind === 0) {
          ind = length - 1;
          play(ind);
        } else {
          ind--;
          play(ind);
        }
      }
    }

    function autoplay(speed) {
      timer = setInterval(function () {
        witchplay(1);
      }, speed);
    }

    autoplay(settings.speed);
    btn.click(function () {
      ind = $(this).index();
      play(ind);
    });
    that.mouseover(function () {
      clearInterval(timer);
      timer = null;
    }).mouseout(function () {
      autoplay(settings.speed);
    });
    return this;
  };
  //无缝滚动
  $.fn.rolling = function (options) {
    var settings = $.extend({
      left: true,
      toLeft: true,
      speed: 1
    }, options);
    var that = this, oLi = that.find('li'), d = 0;

    function clearTime(fn) {
      var timer = setInterval(fn, 25);
      that.hover(function () {
        clearInterval(timer);
        timer = null;
      }, function () {
        timer = setInterval(fn, 25);
      });
    }

    if (settings.left) {
      var wi = oLi.length * oLi.outerWidth(true) * 2;
      that.css({width: wi}).html(that.html() + that.html())
        .parent().css({width: wi / 2, overflow: 'hidden'});
      var move = function () {
        if (settings.toLeft) {
          if (-d >= wi / 2) {
            d = 0;
            that.css('left', 0);
          } else {
            d = d - settings.speed;
            that.css('left', d);
          }
        } else {
          that.css('left', -wi / 2);
          if (d >= wi / 2) {
            d = 0;
            that.css('left', -wi / 2);
          } else {
            d = d + settings.speed;
            that.css('left', d - (wi / 2));
          }
        }
      };
      clearTime(move);
    } else {
      var He = oLi.length * oLi.outerHeight(true) * 2;
      that.css({height: He}).html(that.html() + that.html())
        .parent().css({height: He / 2, overflow: 'hidden'});
      var moveTop = function () {
        if (-d >= He / 2) {
          d = 0;
          that.css('top', 0);
        } else {
          d = d - settings.speed;
          that.css('top', d);
        }
      };
      clearTime(moveTop);
    }
    return this;
  };
  //左右滑动轮播
  $.fn.slideBanner = function (options) {
    var setting = $.extend({
        speed: 3000,
        arrow: true,
        hover: true
      }, options),
      that = this,
      bannerUi = that.find('.banner-ul'),
      bannerLi = bannerUi.find('li'),
      arrow = that.find('.banner-arrow'),
      nextBtn = that.find('.next'),
      prevBtn = that.find('.prev'),
      bannerBtn = that.find('.banner-btn').find('li'),
      timer = null,
      state = true,
      index = 0,
      sMove = bannerUi.outerWidth(),
      liNum = bannerLi.length;
    bannerLi.first().css('left', 0);
    function focus() {
      bannerBtn.removeClass('on');
      bannerBtn.eq(index).addClass('on');
    }

    if (!setting.arrow) {
      arrow.css('display', 'none');
    }
    bannerBtn.on('click', function () {
      var thisIndex = $(this).index();
      if (thisIndex > index) {
        bannerLi.eq(index).animate({left: -sMove});
        bannerLi.eq(thisIndex).css({left: sMove}).animate({left: 0});
        index = thisIndex;
        focus();
      } else if (thisIndex < index) {
        bannerLi.eq(index).animate({left: sMove});
        bannerLi.eq(thisIndex).css({left: -sMove}).animate({left: 0});
        index = thisIndex;
        focus();
      }
    });
    function play() {
      bannerLi.eq(index).animate({left: -sMove});
      index++;
      if (index === liNum) {
        index = 0;
        bannerLi.eq(index).css({left: sMove});
      }
      bannerLi.eq(index).css({left: sMove}).animate({left: 0});
      focus();
    }

    function autoPlay() {
      timer = setInterval(play, setting.speed);
    }

    if (setting.hover) {
      that.hover(function () {
        clearInterval(timer);
        timer = null;
      }, function () {
        autoPlay();
      });
    }
    prevBtn.click(function () {
      if (state === true) {
        state = false;
        bannerLi.eq(index).animate({left: sMove});
        index--;
        if (index < 0) {
          index = liNum - 1;
        }
        bannerLi.eq(index).css({left: -sMove}).animate({left: 0}, function () {
          state = true;
        });
        focus();
      }
    });
    nextBtn.click(function () {
      if (state === true) {
        state = false;
        bannerLi.eq(index).animate({left: -sMove});
        index++;
        if (index === liNum) {
          index = 0;
        }
        bannerLi.eq(index).css({left: sMove}).animate({left: 0}, function () {
          state = true;
        });
        focus();
      }
    });
    autoPlay();
    if (liNum === 1) {
      clearInterval(timer);
      timer = null;
    }
    return this;
  };
  //选项卡
  win.tab = function (btn, box) {
    if (box.length > 0) {
      box.eq(0).css('display', 'block');
      btn.click(function () {
        if (!$(this).hasClass('on')) {
          btn.removeClass('on');
          $(this).addClass('on');
          box.css('display', 'none').eq($(this).index()).fadeIn(200);
        }
      });
    }
    return this;
  };
  //广告浮动
  $.fn.floatAd = function (option) {
    var options = $.extend({
        width: "240",
        height: "150",
        speed: 18
      }, option),
      that = this,
      x = 0,
      y = 0,
      xin = true, yin = true;
    that.css({
      position: 'fixed',
      zIndex: 99999,
      width: options.width,
      height: options.height,
      backgroundColor: '#ffffff'
    });
    $('<span id="close-btn">&times;</span>').css({
      position: 'absolute',
      top: 0,
      right: -20,
      width: 20,
      height: 20,
      lineHeight: '20px',
      fontSize: '18px',
      textAlign: 'center',
      cursor: 'pointer',
      backgroundColor: '#dadada'
    }).appendTo(that);
    var DW = $(window).width(),
      DH = $(window).height(),
      OW = that.width(),
      OH = that.height(),
      L = 0,
      T = 0;

    function float() {
      x = x + (xin ? 1 : -1);
      if (x < L) {
        xin = true;
        x = L;
      }
      if (x > DW - OW - 1) {
        xin = false;
        x = DW - OW - 1;
      }
      y = y + (yin ? 1 : -1);
      if (y > DH - OH - 1) {
        yin = false;
        y = DH - OH - 1;
      }
      if (y < T) {
        yin = true;
        y = T;
      }
      that.css({
        'top': y,
        'left': x
      });
    }

    var timer = setInterval(float, options.speed);
    that.hover(function () {
      clearInterval(timer);
      timer = null;
    }, function () {
      if (that.css('display') !== 'none') {
        timer = setInterval(float, options.speed);
      }
    });
    $('#close-btn').click(function (e) {
      e.stopPropagation();
      that.css('display', 'none');
    });
    return this;
  };
}(jQuery, window));

//转义
(function () {
  function htmlDecode(str) {
    if (str.length === 0) {
      return ""
    } else {
      var s = str.replace(/&gt;/g, "&");
      s = s.replace(/&lt;/g, "<");
      s = s.replace(/&gt;/g, ">");
      s = s.replace(/&nbsp;/g, " ");
      s = s.replace(/&#39;/g, "\'");
      s = s.replace(/&quot;/g, "\"");
      s = s.replace(/<br>/g, "\n");
      return s;
    }
  }

  function htmlEncode(str) {
    if (str.length === 0) {
      return ""
    } else {
      var s = str.replace(/&/g, "&gt;");
      s = s.replace(/</g, "&lt;");
      s = s.replace(/>/g, "&gt;");
      s = s.replace(/ /g, "&nbsp;");
      s = s.replace(/\'/g, "&#39;");
      s = s.replace(/\"/g, "&quot;");
      s = s.replace(/\n/g, "<br/>");
      return s;
    }
  }
}());

/*$('.ad').floatAd({});
$('.notice-list a').limitWords(40);
$('.slide-two').fadingSlide({speed: 3000});
$('ul.rolling').rolling({left: true, toLeft: true, speed: 1});
$('#banner').slideBanner({speed: 3500, arrow: false, hover: false});*/

var yunui={
  yunbox:function (obj) {
    var def={
      title:'信息',
      url:'',
      content:'',
      size:[800,500],
      pos:['ct'],
      minmax:false,
      time:1500,
      btns:['yes','no'],
      showfoot:true,
      onopen:function (body) {},
      onclose:function (body) {},
      onmin:function (body) {},
      yes:function (body) {},
      resize:true
    };
    var opt=$.extend(def,obj);
    var temp=
      ' <div class="yuner">' +
      '    <div class="yunbox-bg"></div>' +
      '    <div class="yunbox-move"></div>' +
      '    <div class="yunbox">' +
      '        <div class="yunbox-content">' +
      '            <div class="yunbox-head-opt">' +
  '                    <a class="yunbox-opt-min" href="javascript:;"></a>' +
  '                    <a class="yunbox-opt-minmiddle" href="javascript:;"></a>' +
  '                    <a class="yunbox-opt-max" href="javascript:;"></a>' +
  '                    <a class="yunbox-opt-middle" href="javascript:;"></a>' +
  '                    <a class="yunbox-opt-close" href="javascript:;"></a>' +
  '                </div>' +
      '            <div class="yunbox-head yunbox-drag">' +
      '                <p>'+opt.title+'</p>' +
      '            </div>' +
      '            <div class="yunbox-body"><div class="yunbox-body-bg"></div></div>' +
      '            <div class="yunbox-foot">' +
      '                <div class="yunbox-changesize"></div>' +
      '                <div class="yunbox-foot-btns">' +
      '                    <a class="yunbox-btn0" href="javascript:;">确认</a>' +
      '                    <a class="yunbox-btn1" href="javascript:;">取消</a>' +
      '                </div>' +
      '            </div>' +
      '        </div>' +
      '    </div>' +
      ' </div>';
    $('body').append(temp);

    var yuner=$('div.yuner');
    var yunbox=yuner.find('div.yunbox');
    var yunboxbody=yunbox.find('div.yunbox-body');
    var movebg=yuner.find('div.yunbox-move');
    var dragDiv=yuner.find('div.yunbox-head');
    var childFrame=null;

    if(opt.url){
      $('<iframe ' +
        'id="yunui-iframe" name="yunui-iframe" frameborder=0' +
        'scrolling="auto" allowtransparency="true">' +
        '</iframe>')
        .addClass('yunbox-body-frame').attr('src',opt.url).appendTo(yunboxbody);
    }else if(opt.content) {
      yunboxbody.html(opt.content);
    }else {
      yunboxbody.html('请填写url参数或者content参数');
    }

    yuner.show();
    // 初始化弹窗尺寸
    yunbox.css({
      width:opt.size[0],
      height:opt.size[1]
    }).addClass('scale1');
// 初始化定位弹出框
    if(opt.pos[0]==='c'){
      var l=($(document).width()-yunbox.outerWidth())/2;
      var t=($(document).height()-yunbox.outerHeight())/2;
      yunbox.css({
        top:t,
        left:l
      });
    }else if(opt.pos[0]==='ct' || opt.pos[0]==='tc'){
      l=($(document).width()-yunbox.outerWidth())/2;
      yunbox.css({
        top:0,
        left:l
      });
    }else {
      yunbox.css({
        left:opt.pos[0],
        top:opt.pos[1]
      });
    }
    $(window).on('resize',function () {
      yunbox.css({
        top:($(this).height()-yunbox.outerHeight())/2,
        left:($(this).width()-yunbox.outerWidth())/2
      });
    });

// 调整窗口大小：右下角
    if(opt.resize){
      yunbox.on('mousedown','div.yunbox-changesize',function (e) {
        e.stopPropagation();
        e.preventDefault();
        movebg.show().css('cursor','nw-resize').on('mousemove',function (e) {
          e.stopPropagation();
          e.preventDefault();
          var w=e.pageX-yunbox.offset().left;
          var h=e.pageY-yunbox.offset().top;
          if(w<200){w=200;}
          if(h<200){h=200;}
          yunbox.css({
            width:w,
            height:h
          });
        }).on('mouseup',function (e) {
          e.stopPropagation();
          e.preventDefault();
          $(this).hide().off('mousemove').off('mouseup');
        });
      })
    }else {yunbox.find('div.yunbox-changesize').hide();}
   /* $(document).on('click',function () {
      console.log('doc');
    });*/

// 拖动弹出框
    yunbox.on('mousedown','div.yunbox-drag',function (e) {
      e.stopPropagation();
      e.preventDefault();
      var l=e.pageX-$(this).offset().left;
      var t=e.pageY-$(this).offset().top;
      movebg.show().css('cursor','move').on('mousemove',function (e) {
        e.stopPropagation();
        e.preventDefault();
        yunbox.css({
          left:e.pageX-l,
          top:e.pageY-t
        });
      }).on('mouseup',function (e) {
        e.stopPropagation();
        e.preventDefault();
        $(this).hide().off('mousemove').off('mouseup');
      });
    });
    $(window).on('mouseup',function (e) {
      e.stopPropagation();
      e.preventDefault();
      movebg.hide().off('mousemove').off('mouseup');
    });


// 是否显示底部的按钮组
    if(!opt.showfoot){
      yunbox.find('div.yunbox-foot').hide();
    }

    // 打开弹出框回调
    $('#yunui-iframe').on('load',function () {
      yunbox.find('div.yunbox-body-bg').fadeOut(100);
      try{
        childFrame=$(this).contents();
        opt.onopen(childFrame);
      }catch (err){console.log(err)}
    });
//      关闭弹出层方法
    function close(obj) {
      opt.onclose(obj);
      yuner.fadeOut(300,function () {
        $(this).remove();
      });
      /*yunbox.removeClass('scale1').then(function () {
        yuner.remove();
      });*/
    }
//      取消按钮
    yunbox.on('click','a.yunbox-btn1',function (e) {
      e.stopPropagation();
      close(childFrame);
    });

//      确认按钮
    yunbox.on('click','a.yunbox-btn0',function (e) {
      e.stopPropagation();
      opt.yes(childFrame);
    });
//      关闭按钮
    yunbox.on('click','a.yunbox-opt-close',function (e) {
      e.stopPropagation();
      close(childFrame);
    });

    (function () {
      var tt,ll,ww,hh,isMax=false;
      //      显示最大化最小化按钮
      if(!opt.minmax){
        yunbox.find('a.yunbox-opt-min').hide();
        yunbox.find('a.yunbox-opt-max').hide();
        yunbox.find('a.yunbox-opt-middle').hide();
        yunbox.find('a.yunbox-opt-minmiddle').hide();
      }
      yunbox.find('a.yunbox-opt-max').hide();
      yunbox.find('a.yunbox-opt-minmiddle').hide();

      //      最小化按钮
      yunbox.on('click','a.yunbox-opt-min',function (e) {
        e.stopPropagation();
        opt.onmin(childFrame);
        tt=yunbox.css('top');
        ll=yunbox.css('left');
        ww=yunbox.css('width');
        hh=yunbox.css('height');
        $(this).hide().siblings('.yunbox-opt-max').hide()
          .siblings('.yunbox-opt-middle').hide()
          .siblings('.yunbox-opt-minmiddle').show();
        if(isMax){
          yunbox.animate({
            top: 0,
            left: 0,
            width: 210,
            height: 42
          },200,function () {
            yuner.find('div.yunbox-bg').hide().end()
              .find('div.yunbox-body').hide().end()
              .find('div.yunbox-foot').hide();
          });
          $(this).hide().siblings('.yunbox-opt-middle').show()
            .siblings('.yunbox-opt-minmiddle').hide();
          isMax=false;
        }else {
          yunbox.animate({
            top: 0,
            left: 0,
            width: 210,
            height: 42
          },200,function () {
            yuner.find('div.yunbox-bg').hide().end()
              .find('div.yunbox-body').hide().end()
              .find('div.yunbox-foot').hide();
          });
        }
        yunbox.find('div.yunbox-changesize').hide();
        dragDiv.addClass('yunbox-drag');
      });
      // 最小化到中间按钮
      yunbox.on('click','a.yunbox-opt-minmiddle',function (e) {
        e.stopPropagation();
        $(this).hide().siblings('.yunbox-opt-min').show()
          .siblings('.yunbox-opt-middle').show();
        yuner.find('div.yunbox-bg').show().end()
          .find('div.yunbox-body').show().end()
          .find('div.yunbox-foot').show();
        yunbox.animate({
          width:ww,
          height:hh,
          top:tt,
          left:ll
        },200);
        yunbox.find('div.yunbox-changesize').show();
      });
//      中间到最大化按钮
      yunbox.on('click','a.yunbox-opt-middle',function (e) {
        e.stopPropagation();
        tt=yunbox.css('top');
        ll=yunbox.css('left');
        ww=yunbox.css('width');
        hh=yunbox.css('height');
        yuner.find('div.yunbox-bg').show().end()
          .find('div.yunbox-body').show().end()
          .find('div.yunbox-foot').show().end()
          .find('a.yunbox-opt-min').show();
        isMax=true;
        yunbox.animate({
          width:'100%',
          height:'100%',
          left:0,
          top:0
        },200);
        $(this).hide().prev().show();
        yunbox.find('div.yunbox-changesize').hide();
        dragDiv.removeClass('yunbox-drag');
      });
      // 最大化到中间按钮
      yunbox.on('click','a.yunbox-opt-max',function (e) {
        e.stopPropagation();
        $(this).hide().next().show();
        if(isMax){
          ww=opt.size[0];
          hh=opt.size[1];
          tt=($(window).height()-opt.size[1])/2;
          ll=($(window).width()-opt.size[0])/2;
          isMax=false;
          yunbox.animate({
            width:ww,
            height:hh,
            top:tt,
            left:ll
          },200);
        }else {
          yunbox.animate({
            width:ww,
            height:hh,
            top:tt,
            left:ll
          },200);
        }
        yunbox.find('div.yunbox-changesize').show();
        dragDiv.addClass('yunbox-drag');
      });

    })();


  }


};

